<template>
  <div>
    <mt-field label="账号" placeholder="账号" type="tel" v-model="number"></mt-field>
    <mt-field label="密码" placeholder="请输入密码" type="password" v-model="password"></mt-field>
      <!--<router-link to="/home">-->
        <mt-button type="default" class="set-button1" @click="HandleLogin">登陆</mt-button>
      <!--</router-link>-->
    <mt-button type="default" class="set-button2" @click="btnChange">重置</mt-button>
    <router-link to="/Register">
      <div>忘记密码？</div>
    </router-link>
    <router-link to="/Register">
      <div>新建账户？</div>
    </router-link>
  </div>
</template>

<script>
  import { Button } from 'mint-ui';
  import { Field } from 'mint-ui';
  import { MessageBox } from 'mint-ui';
  import axios from 'axios'

  export default {
    name: "LoginContent",
    props:{
      userMessage:Array
    },
    components:{
      MtField:Field,
      MtButton:Button,
    },
    data:function () {
      return{
        userList:[],
        password:'',
        number:'',
      }
    },
    methods:{
      btnChange:function () {
        this.password = null;
        this.number = null;
      },

      LoginInfo: function() {
        axios.post('/api/student')
          .then(this.getLoginInfoSucc)
          .catch(err => {
            console.log(err);
          });
      },
      getLoginInfoSucc:function (res) {
        console.log(res);
        for(var i = 1;i < res.data.length;i++){
        }
      },
      HandleLogin:function () {
        //账号一致就登陆否则就反馈信息
        var _this = this;
        if(Number(this.number)<1){
          MessageBox("账号不能为空");
        }
        if(Number(this.number)==0&&Number(this.password)>0){
          MessageBox('请先输入账号');
        }else{
          console.log('获取数据库');
          axios.get('http://47.106.193.107:8080/student')
            .then(function (response) {
              console.log(response.data);
              _this.my = response.data;
              console.log("数据response的长度是：",response.data.length);
              for (var i = 0;i<response.data.length;i++){
                if (Number(_this.number)==response.data[i].number){
                  console.log("账号正确");
                  if(Number(_this.password)==response.data[i].password){
                    console.log("登陆成功");
                    _this.$router.push('/home');
                    _this.$store.state.userName = response.data[i].name;
                    console.log(response.data[i].number);
                    console.log(response.data[i].password);
                    break;
                  }else{
                    MessageBox('密码错误');
                    break;
                  }
                } else{
                  if(i == response.data.length-1){
                    MessageBox('这个账号没有注册');
                  }
                }
              }
            })
            .catch(function (error) {
              console.log(error);
            });
        }
        }
    },
    mounted () {
      this.LoginInfo();
    }
  }
</script>

<style scoped>
  .set-button1{
    margin-top: 1rem;
    margin-left: 0rem;
    margin-right: 6.5rem;

  }
  .set-button1{
    margin-top: 1rem;
    margin-right: 3rem;
  }
</style>
